import request from '@/utils/request'
// , setToken, removeToken
import { getToken } from '@/utils/auth'

export function post(url, path, data, headers) {
  headers = headers || { 'Content-Type': 'application/json; charset=UTF-8', 'Accept': 'application/json' }
  console.log(headers)

  var transformRequest = []
  if (headers && headers['Content-Type'] && headers['Content-Type'].indexOf('application/x-www-form-urlencoded') !== -1) {
    transformRequest = [function(data) {
      if (headers && headers['Content-Type'].indexOf('application/x-www-form-urlencoded') !== -1) {
        console.log('---------------------------------------------')
        let ret = ''
        for (const it in data) {
          ret += encodeURI(it) + '=' + encodeURI(data[it]) + '&'
        }
        if (ret) ret = ret.substring(0, ret.length - 1)
        return ret
      }
      // return data
    }]
  }

  return request({
    url: url + getToken() + path,
    method: 'post',
    transformRequest,
    data,
    headers

  })
}

// transformRequest: transformRequest,

export function get(url, path, data, headers) {
  headers = headers || { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept': 'application/json' }
  console.log(data)
  url = url + getToken() + path
  if (data) {
    url += '?'
    for (var key in data) {
      url += key + '=' + encodeURI(data[key] !== null ? data[key] : '') + '&'
    }
  }
  console.log(url)
  return request({
    url: url,
    method: 'get',

    headers
  })
}

// packge from data
export function assemblesFromData(data) {
  var fromData = new FormData()
  if (data) {
    for (var prop in data) {
      if (data[prop] !== null) {
        fromData.append(prop, data[prop])
      } else {
        fromData.append(prop, '')
      }
    }
  }
  if (fromData) {
    return fromData
  } else {
    return new FormData()
  }
}

// init module height
export function initModuleHeight(height, module) {
  if (height && height > 0) {
    if (module) {
      for (var prop in module) {
        if (module[prop] !== null) {
          if (module[prop].child && typeof module[prop].child !== undefined) {
            module[prop].height = Math.floor(height * module[prop].size)
            const _height = module[prop].height
            const _module_2 = module[prop].child
            for (var _prop_2 in _module_2) {
              _module_2[_prop_2].height = Math.floor(_height * _module_2[_prop_2].size)
            }
          } else {
            module[prop].height = Math.floor(height * module[prop].size)
            continue
          }
        } else {
          continue
        }
      }
    }
    return module
  }
}

// 下载文件，data类型为blob
export function download(data, fileType, fileName) {
  if (!data) {
    return
  }
  const url = window.URL.createObjectURL(new Blob([data], { type: fileType }))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
}

// 判断是否为图片文件
export function isAssetTypeAnImage(ext) {
  return [
    'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff']
    .indexOf(ext.toLowerCase()) !== -1
}
